<?php
    defined('BASEPATH') OR exit('No direct script access allowed');

    /**
     * @property  Goods_model   goods_model
     * @property  Eccang        eccang
     * @property  good          good
     * @property  siteweb_model siteweb_model
     */
    class Baby extends Admin_Controller
    {

        public function baby_goods_list(){
            $this->load->model('baby_model');
            $this->load->library('pagination');
            $assoc = $this->uri->uri_to_assoc();
            $_param = array(
                'page'           => $assoc['page'] > 0 ? intval($assoc['page']) - 1 : 0,
                'baby_goods_name'     => trim($this->input->get('baby_goods_name')),
            );

            $config['base_url']         = base_url('Baby/') . 'baby_goods_list/page/';
            $config['total_rows']       = $this->baby_model->baby_goods_num($_param);
            $config['num_links']        = 6;
            $config['per_page']         = $this->baby_model->limit_rows;
            $config['use_page_numbers'] = true;
            $this->pagination->initialize($config);

            $baby_goods = $this->baby_model->baby_goods_list($_param);
            foreach($baby_goods as $k => $v){
                $shopify = $this->db->from('baby_shopify_goods')->where(['baby_goods_id'=>$v['id']])->get()->result_array();
                $shopify_a = [];
                foreach ($shopify as $key => $value) {
                    $siteweb = $this->db->from('siteweb')->where(['web_id'=>$value['web_id']])->select('web_name')->get()->row_array();
                    $siteweb['time'] = date('Y-m-d',$value['add_time']);
                    $siteweb['web_id'] = $value['web_id'];
                    $shopify_a[] = $siteweb;
                }
                $baby_goods[$k]['shopify'] = $shopify_a;
                    $suppliers = $this->db->from('suppliers')->where(['suppliers_id'=>$v['baby_gooda_supplier']])->select('suppliers_name')->get()->row_array();
                    $baby_goods[$k]['baby_gooda_supplier'] = $suppliers['suppliers_name'];
                    $baby_goods_img = $this->baby_model->baby_img($v['id']);
                    $baby_goods[$k]['baby_goods_img'] = $baby_goods_img;
                $baby_goods_sku = $this->db->from('baby_goods_sku')->where(['baby_goods_id'=>$v['id'],'baby_is_delete'=>0,'baby_e_update'=>0])->get()->row_array();
                if($baby_goods_sku){
                    $baby_goods[$k]['baby_e_update'] = 1;
                }
            }
            $this->load->model('siteweb_model');
            $siteweb        = $this->siteweb_model->siteweb_option(1, array('type' => 1));
            $_action_link = array(
                array('href' => base_url('Baby/baby_goods'), 'name' => '添加产品'),
            );
            $_param_option = array(
                'baby_goods_name'  => array(
                    'col'         => 'col-lg-3',
                    'type'        => 'text',
                    'placeholder' => '输入产品名称',
                ),
            );
            $data = array(
                'siteweb'        => $siteweb,
                'siteweb_json'   => json_encode($siteweb),
                'pages'          => $this->pagination->create_links(),
                'param_option'     => $_param_option,
                'baby_goods' => $baby_goods,
                'action_link'    => $_action_link,
                'param'          => $_param,
            );
            $this->load->view('baby/baby_goods_list', $data);
        }

        public function baby_goods(){
            $this->config->load('website_common');
            $this->load->library('session');
            $this->load->model('baby_model');
            $this->load->model('admin_model');
            $this->load->model('suppliers_model');
            // $this->load->model('size_model');
            // $this->load->model('color_model');
            $this->load->library('sys_message');
            $config = $this->config->item('upload');
            $this->load->library('upload', $config);
            $this->load->library('image_lib');
            $goods_sn         = $this->baby_model->goods_sn_max($this->session->userdata('admin_id'),0);
            if ($_POST){
                $files_sort = $this->input->post('files_sort');
                !$files_sort && $this->sys_message->wrong('请上传图片!');
                $sku = $this->input->post('sku');
                !array_filter($sku['size']) && $this->sys_message->wrong('请选择尺码!');
                !array_filter($sku['color']) && $this->sys_message->wrong('请选择颜色!');
                $data = array(
                    'baby_goods_name'       => trim($this->input->post('cn_name')), //商品名称
                    'baby_channel'          => trim($this->input->post('channel_id')),  //渠道
                    'baby_gooda_supplier'   => trim($this->input->post('suppliers_id')), //供应商ID
                    'baby_admin_id'         => trim($this->input->post('admin_id')), //添加人ID
                    'baby_goods_sn'         => $goods_sn,//货号
                    'baby_goods_link'       => $this->input->post('supp_url'),//商品链接
                    'baby_goods_enter_price'=> $this->input->post('in_price'),//进价
                    'baby_goods_price'      => $this->input->post('shop_price'),//售价
                    'baby_goods_describe'   => $this->input->post('summernote'),//描述
                    'baby_add_time'         => time(),//添加时间
                    'purchase_id'           => $this->input->post('purchase_id'),//采购员
                );
                $this->db->insert('baby_goods',$data);
                $id = $this->db->insert_id();


                
                foreach ($files_sort as $k => $val) {
                    if ($this->upload->do_upload('files' . $k)){
                        $data_images            = $this->upload->data();
                        $img['baby_goods_img'] = $data_images['file_name'];
                        $img['baby_add_time'] = time();
                        $img['baby_goods_id'] = $id;
                        $this->db->insert('baby_goods_img',$img);
                    }
                }

                
                foreach ($sku['size'] as $key => $val) {
                    if($val){
                        foreach ($sku['color'] as $k => $v){
                            if($v){
                                $size_value = $this->db->from('size')->where(['size_id'=>$val])->select('size_id,new_size_code')->get()->row_array();
                                // if($v == 01){
                                //     $head = 'Stationary';
                                // }else{
                                //     $head = 'Bobble';
                                // }
                                $baby_sku['baby_sku_head']  = $v;
                                $baby_sku['baby_sku_size']  = $size_value['size_id'];
                                $baby_sku['baby_goods_id']  = $id;
                                $baby_sku['baby_goods_sku'] = $goods_sn.rand(100,999).$size_value['new_size_code'].$v;
                                $baby_sku['sku_weight']     = $sku['sku_weight'][$key];
                                $baby_sku['size_price']     = $sku['size_price'][$key];
                                $baby_sku['head_price']     = $sku['head_price'][$k];
                                $baby_sku['head_size_price']= $this->input->post('shop_price') + $sku['size_price'][$key] + $sku['head_price'][$k];
                                $this->db->insert('baby_goods_sku',$baby_sku);
                            }
                        }
                    }
                }
                $this->sys_message->success('成功!');
                



            }else{
                $size_option = $this->db->from('size')->where(['type'=>2])->select('size_id,size_value')->get()->result_array();
                $suppliers_option = $this->suppliers_model->suppliers_option();
                $admin_id         = $this->session->userdata('admin_id');
                $admin_name       = $this->admin_model->admin_option();
                $purchase = $this->db->from('purchase')->where(['is_delete' => 0])->get()->result_array();
                $data = array(
                    'purchase'         => $purchase,
                    'size_option'      => $size_option,
                    'suppliers_option' => $suppliers_option,
                    'goods_sn'         => $goods_sn,
                    'admin_name'       => $admin_name,
                    'admin_id'         => $admin_id,
                );

                $this->load->view('baby/baby_goods', $data);
            }
        }

        public function baby_goods_save(){
            if($_POST){
                $this->config->load('website_common');
                $config = $this->config->item('upload');
                $this->load->library('upload', $config);
                $this->load->library('image_lib');


                $this->load->library('sys_message');
                
                $goods_id = $this->input->post('baby_id');
                
                $attr      = array(
                    'attr_title' => $this->input->post('attr_title'),
                    'attr_val'   => $this->input->post('attr_val'),
                );
                $data = array(
                    'baby_goods_name'       => trim($this->input->post('cn_name')), //商品名称
                    'baby_channel'          => trim($this->input->post('channel_id')),  //渠道
                    'baby_gooda_supplier'   => trim($this->input->post('suppliers_id')), //供应商ID
                    'baby_goods_link'     => $this->input->post('supp_url'),//商品链接
                    'baby_goods_enter_price'         => $this->input->post('in_price'),//进价
                    'baby_goods_price'     => $this->input->post('shop_price'),//售价
                    'baby_goods_describe'         => $this->input->post('summernote'),//描述
                    'baby_add_time'          => time(),//添加时间
                    'purchase_id'          => $this->input->post('purchase_id'),//采购员
                    'cat_id'       => $this->input->post('cat_id'), //商品分类
                );
                $this->db->where(['id'=>$goods_id]);
                $this->db->update('baby_goods',$data);


                //图片处理
                // $baby_goods_img = $this->db->from('baby_goods_img')->where(['baby_goods_id'=>$goods_id])->select('id,baby_goods_img')->get()->result_array();
                // foreach ($baby_goods_img as $key => $val) {
                //     $img[$val['id']] = $val['baby_goods_img'];
                // }
                $files_sort = $this->input->post('files_sort');
                !$files_sort && $this->sys_message->wrong('请上传图片!');
                // print_r($files_sort);die;
                $this->db->where(['baby_goods_id'=>$goods_id]);
                $this->db->update('baby_goods_img',['baby_is_delete'=>1]);
                foreach ($files_sort as $k => $val) {

                    if ($val){
                        $this->db->where(['id'=>$val]);
                        $this->db->update('baby_goods_img',['baby_is_delete'=>0]);
                        $baby_goods_img = $this->db->from('baby_goods_img')->where(['id'=>$val])->select('baby_goods_img')->get()->row_array();
                        $files_sort[$k] = $baby_goods_img['baby_goods_img'];
                        // $sku_img_data = array(
                        //     'file_name' => $img[$val],
                        //     'album_id'  => $val,
                        // );
                        // $data['update'][]       = $sku_img_data;
                    } else {
                        if ($this->upload->do_upload('files' . $k)){
                            $data_images            = $this->upload->data();
                            $image['baby_goods_img'] = $data_images['file_name'];
                            $image['baby_add_time'] = time();
                            $image['baby_goods_id'] = $goods_id;
                            $this->db->insert('baby_goods_img',$image);
                            $files_sort[$k] = $data_images['file_name'];
                        }
                    }
                }
                //SKU处理
                $sku = $this->input->post('sku');
                $this->db->where(['baby_goods_id'=>$goods_id]);
                $this->db->update('baby_goods_sku',['baby_is_delete'=>1]);
                foreach ($sku['size'] as $k => $v) {
                    $info = $this->db->from('baby_goods_sku')->where(['baby_sku_head'=>$sku['color'][$k],'baby_sku_size'=>$v,'baby_goods_id'=>$goods_id])->get()->row_array();
                    if($info){
                        $arr['sku_weight'] = $sku['sku_weight'][$k];
                        $arr['size_price'] = $sku['size_price'][$k];
                        $arr['head_price'] = $sku['head_price'][$k];
                        $arr['head_size_price'] = $this->input->post('shop_price') + $sku['size_price'][$k] + $sku['head_price'][$k];
                        $arr['baby_is_delete'] = 0;
                        $this->db->where(['id'=>$info['id']]);
                        $this->db->update('baby_goods_sku',$arr);
                    }else{
                        $new_size_code  = $this->db->from('size')->where(['size_id'=>$v])->select('new_size_code')->get()->row_array();
                        $baby_sku = $this->input->post('baby_goods_sn').rand(100,999).$sku['color'][$k].$new_size_code['new_size_code'];
                        $res['baby_sku_head'] = $sku['color'][$k];
                        $res['baby_sku_size'] = $v;
                        $res['baby_add_time'] = time();
                        $res['baby_goods_sku'] = $baby_sku;
                        $res['baby_goods_id'] = $goods_id;
                        $res['sku_weight'] = $sku['sku_weight'][$k];
                        $res['size_price'] = $sku['size_price'][$k];
                        $res['head_price'] = $sku['head_price'][$k];
                        $res['head_size_price'] = $this->input->post('shop_price') + $sku['size_price'][$k] + $sku['head_price'][$k];
                        $this->db->insert('baby_goods_sku',$res);
                    }
                }
                $this->sys_message->success('成功!');
            }
            $this->load->model('suppliers_model');
            $this->load->model('admin_model');
            $assoc = $this->uri->uri_to_assoc();
            $this->load->model('baby_model');
            $baby_id = intval($assoc['baby_id']);
            $baby_goods = $this->db->from('baby_goods')->where(['id'=>$baby_id])->get()->row_array();
            $baby_goods['baby_goods_img'] = $this->db->from('baby_goods_img')->where(['baby_goods_id'=>$baby_goods['id'],'baby_is_delete'=>0])->select('id,baby_goods_img')->get()->result_array();
            $baby_goods['baby_goods_sku'] = $this->db->from('baby_goods_sku')->where(['baby_goods_id'=>$baby_goods['id'],'baby_is_delete'=>0])->select('baby_sku_head,baby_sku_size,size_price,sku_weight,head_price,head_size_price,baby_goods_sku')->get()->result_array();
            $size_option = $this->db->from('size')->where(['type'=>2])->select('size_id,size_value')->get()->result_array();
            $purchase = $this->db->from('purchase')->where(['is_delete' => 0])->get()->result_array();
            $data = array(
                'size_option'      => $size_option,
                'suppliers_option' => $this->suppliers_model->suppliers_option(),
                'baby_goods' => $baby_goods,
                'admin_name' => $this->admin_model->admin_option(),
                'purchase' => $purchase,
            );
            $this->load->view('baby/baby_goods_save', $data);
        }

        public function baby_ww_goods_e_sku(){
            $goods_id = $this->input->post('goods_id');
            $baby_goods_sku = $this->db->from('baby_goods_sku')->where(['baby_goods_id'=>$goods_id,'baby_is_delete'=>0,'baby_e_update'=> 0])->get()->result_array();
            foreach ($baby_goods_sku as $key => $value) {
                $size_code = $this->db->from('size')->where(['size_id'=>$value['baby_sku_size']])->select('size_code')->get()->row_array();
                //$color_zh = $this->db->from('colors')->where(['color_id'=>$value['baby_sku_head']])->select('color_zh')->get()->row_array();
                if($value['baby_sku_head'] == 01){
                    $option = '不摇头';
                }else{
                    $option = '摇头';
                }
                $baby_goods_sku[$key]['baby_sku_size'] = $size_code['size_code'];
                $baby_goods_sku[$key]['baby_sku_head'] = $option;
                $baby_goods_img = $this->db->from('baby_goods_img')->where(['baby_is_delete'=>0,'baby_goods_id'=>$value['baby_goods_id']])->get()->row_array();
                $baby_goods_sku[$key]['baby_goods_img'] = $baby_goods_img['baby_goods_img'];
            }
            echo json_encode($baby_goods_sku);
        }

        public function pets_ww_goods_sku_examine(){

            $this->load->library('sys_message');
            if ( !$_SESSION['admin_id']){
                $this->sys_message->wrong('刷新页面');
            }
            if ($_SESSION['admin_id'] != 2 && $_SESSION['admin_id'] != 20 && $_SESSION['admin_id'] != 14){
                $this->sys_message->wrong('负责人才能审核');
            }

            $goods_id = $this->input->post('goods_id');
            if (empty($goods_id)){
                $this->sys_message->wrong('刷新页面');
            }
            $array = $this->db->from('baby_goods_sku')->where(['baby_goods_id' => $goods_id, 'baby_is_delete' => 0,'baby_e_update'=> 0])->get()->result_array();
            foreach ($array as $k => $v) {
                $baby_goods = $this->db->from('baby_goods')
                               ->where(['id' => $v['baby_goods_id']])
                               ->get()->row_array();
                $type       = $this->db->from('goods_type')
                                           ->where(['cat_id' => $baby_goods['cat_id']])
                                           ->select('cat_name_zh,cat_name,cate_code')
                                           ->get()->row_array();
                $suppliers_name = $this->db->from('suppliers')
                               ->where(['suppliers_id' => $baby_goods['baby_gooda_supplier']])
                               ->select('suppliers_name,suppliers_id')
                               ->get()->row_array();
                $goods_album = $this->db->from('baby_goods_img')
                        ->where(['baby_goods_id'=>$v['baby_goods_id'],'baby_is_delete'=>0])
                        ->select('baby_goods_img')
                        ->get()->row_array();
                $this->load->library('eccang');
                $info = $this->eccang->getSupplierInfo(['condition' => ['supplierName' => trim($suppliers_name['suppliers_name'])]]);
                if ( !empty($info['data'])){
                    $defaultSupplierCode = $info['data'][0]['supplierCode'];
                } else {
                    $defaultSupplierCode = 'G' . $suppliers_name['suppliers_id'];
                    $data                = [
                        'actionType' => 'ADD',
                        'supplier'   => [
                            'supplierCode'         => $defaultSupplierCode,
                            'supplierName'         => $suppliers_name['suppliers_name'],
                            'buyerId'              => $suppliers_name['suppliers_id'] % 2 == 1 ? 583 : 609,
                            'level'                => 'A',
                            'supplierTeamworkType' => 0,
                            'supplierType'         => 1,
                            'pcId'                 => 3,
                            'accountType'          => 2,
                            'payType'              => 1,
                            'supplierCarrier'      => 1,
                            'shippingMethodIdHead' => 2,
                            'supplierQcException'  => 2,
                            'accountProportion'    => 100,
                        ],
                    ];
                    $suppliers_e         = $this->eccang->syncSupplierInfo($data);
                    $this->db->where(['suppliers_id' => $suppliers_name['suppliers_id']]);
                    $this->db->update('suppliers', ['e_code' => $suppliers_e['data']['supplierCode']]);
                }

                if($v['baby_sku_head'] == 01){
                    $option = '不摇头';
                    $options = '209';
                }else{
                    $option = '摇头';
                    $options = '208';
                }
                $size      = $this->db->from('size')->where(['size_id' => $v['baby_sku_size']])->select('size_code,e_id')->get()->row_array();
                $res       = [];
                $res['actionType']                 = 'ADD';
                $res['productSku']                 = $v['baby_goods_sku'];
                $res['productTitle']               = $baby_goods['baby_goods_name'] . '---' . $option . '/' . $size['size_code'];
                $res['productTitleEn']             = $baby_goods['baby_goods_name'];
                $res['pdOverseaTypeCn']            = $type['cat_name_zh'];
                $res['pdOverseaTypeEn']            = $type['cat_name'];
                $res['productDeclaredValue']       = 1.5;
                $res['pdDeclareCurrencyCode']      = 'USD';
                $res['productPurchaseValue']       = (Float)$baby_goods['baby_goods_enter_price'];
                $res['productPurchaseValueNotTax'] = (Float)$baby_goods['baby_goods_enter_price'];
                $res['currencyCode']               = 'RMB';
                $res['productWeight']              = $v['sku_weight'] < 50 ? 0.05 : $v['sku_weight'] / 1000;
                $res['defaultSupplierCode']        = $defaultSupplierCode;
                $res['productPrice']               = (Float)$v['head_size_price'];
                $res['oprationType']               = 2;
                $res['productStatus']              = 1;
                $res['saleStatus']                 = 2;
                $res['hsCode']                     = $type['cate_code'];
                $res['productLength']              = 1;
                $res['productWidth']               = 1;
                $res['productHeight']              = 1;
                $res['personOpraterId']            = $baby_goods['purchase_id'];
                $res['productColorId']             = $options;
                $res['productSizeId']              = $size['e_id'];
                $res['spMinQty']                   = 1;
                $res['spProductAddress']           = $baby_goods['baby_goods_link'];
                $res['productImgUrlList']          = ['http://sumerbikini.com'.$goods_album['baby_goods_img']];
                $info                              = $this->eccang->add_syncProduct($res);
                if ($info['code'] == 200 || ($info['code'] == 500 && $info['error'][0]['errorMsg'] == '产品' . $v['baby_goods_sku'] . '已经存在')){//处理e仓接口易仓导致商品已经创建成功
                    $this->db->where(['id' => $v['id']]);
                    $this->db->update('baby_goods_sku', ['baby_e_update' => 1]);
                }
            }
            $this->sys_message->success('成功');
        }

        public function baby_goods_shopify(){
            $this->load->library('sys_message');
            $this->load->model('siteweb_model');
            $this->load->library('shopify_api');
            $web_id = $this->input->post('web_id');
            $baby_goods_id  = $this->input->post('baby_goods_id');
            $baby_goods = $this->db->from('baby_goods')->where(['id'=>$baby_goods_id])->get()->row_array();
            $goods_sku = $this->db->from('baby_goods_sku')->where(['baby_goods_id' => $baby_goods_id, 'baby_is_delete' => 0])->get()->result_array();
            if(!$goods_sku){
                $this->sys_message->wrong('先上传SKU!');
            }
            if (empty($web_id)){
                $this->sys_message->wrong('请选择站点!');
            }
    
            foreach ($goods_sku as $key => $value) {
                $size_value = $this->db->from('size')->where(['size_id'=>$value['baby_sku_size']])->select('size_value')->get()->row_array();
                $sku[$key]['option1'] = $size_value['size_value'];
                if($value['baby_sku_head'] == 01){
                    $option2 = 'Bobble';
                }else{
                    $option2 = 'Stationary';
                }
                $sku[$key]['option2'] = $option2;
                $sku[$key]['sku']     = $value['baby_goods_sku'];
                $sku[$key]['price']   = $value['head_size_price'];
            }
            $yaotou['name'] = 'yaotou';
            foreach ($goods_sku as $k => $v) {
                if($v['baby_sku_head'] == 01){
                    $option2 = 'Bobble';
                }else{
                    $option2 = 'Stationary';
                }
                $yaotou['values'][] = $option2;
            }
            $size['name'] = 'size';
            foreach ($goods_sku as $k => $v) {
                $size_value = $this->db->from('size')->where(['size_id'=>$v['baby_sku_size']])->select('size_value')->get()->row_array();
                $size['values'][] = $size_value['size_value'];
            }
            $options[] = $yaotou;
            $options[] = $size;
            $baby_goods_img = $this->db->from('baby_goods_img')->where(['baby_goods_id'=>$baby_goods_id,'baby_is_delete'=>0])->get()->result_array();
            foreach ($baby_goods_img as $key => $value) {
                $img[]['src'] = $value['baby_goods_img'];
            }

            foreach ($web_id as $web_key => $web_val) {
                $baby_shopify_goods = $this->db->from('baby_shopify_goods')->where(['web_id'=>$web_val,'baby_goods_id'=>$baby_goods_id])->get()->row_array();
                if($baby_shopify_goods){
                    continue;
                }
                $val['baby_goods_id'] = $baby_goods_id;
                $val['web_id'] = $web_val;
                $val['goods_name'] = $baby_goods['baby_goods_name'];
                $val['goods_sn'] = $baby_goods['baby_goods_sn'];
                $val['goods_price'] = $baby_goods['baby_goods_price'];
                $val['variants'] = json_encode($sku);
                $val['add_time'] = time();
                $this->db->insert('baby_shopify_goods',$val);
                $id = $this->db->insert_id();
                $siteweb = $this->siteweb_model->siteweb_info(array('web_id' => $web_val));
                $this->shopify_api->initialize($siteweb);
                $baby_shopify_data['baby_goods_name'] = $baby_goods['baby_goods_name'];
                $baby_shopify_data['baby_body_html'] = $baby_goods['baby_goods_describe'];
                $baby_shopify_data['variants'] = $sku;
                $baby_shopify_data['images'] = $img;
                $baby_shopify_data['options'] = $options;
                // print_R($baby_shopify_data);die;
                $info = $this->shopify_api->baby_upload_goods($baby_shopify_data);
                $this->db->where(['id'=>$id]);
                $this->db->update('baby_shopify_goods',['shopify_id'=>$info['id']]);
            }
            $this->sys_message->success('成功!');
        }

    public function baby_goods_e_sku(){
        $goods_id = $this->input->post('goods_id');
        $baby_goods_sku = $this->db->from('baby_goods_sku')->where(['baby_goods_id'=>$goods_id,'baby_is_delete'=>0,'baby_e_update'=> 0])->get()->result_array();
        foreach ($baby_goods_sku as $key => $value) {
            $size_value = $this->db->from('size')->where(['size_id'=>$value['baby_sku_size']])->select('size_value')->get()->row_array();
            $color_zh = $this->db->from('colors')->where(['color_id'=>$value['baby_sku_head']])->select('color_zh')->get()->row_array();
            $baby_goods_sku[$key]['baby_sku_size'] = $size_value['size_value'];
            $baby_goods_sku[$key]['baby_sku_head'] = $color_zh['color_zh'];
            $baby_goods_img = $this->db->from('baby_goods_img')->where(['baby_is_delete'=>0,'baby_goods_id'=>$value['baby_goods_id']])->get()->row_array();
            $baby_goods_sku[$key]['baby_goods_img'] = $baby_goods_img['baby_goods_img'];
        }
        echo json_encode($baby_goods_sku);
    }

    public function syncProduct_ww_attr_img()
    {
        $this->load->library('sys_message');
        $this->load->library('eccang');
        $goods_id = $this->input->post('goods_id');
        $goods_sku = $this->db->from('baby_goods_sku')->where(['baby_goods_id' => $goods_id])->get()->result_array();
        $goods = $this->db->from('baby_goods')->where(['id' => $goods_id])->select('baby_goods_price,cat_id,baby_goods_name,baby_gooda_supplier,purchase_id,baby_goods_link')->get()->row_array();
        $goods_type = $this->db->from('goods_type')->where(['cat_id' => $goods['cat_id']])->select('cat_name,cat_name_zh')->get()->row_array();
        $baby_goods_img = $this->db->from('baby_goods_img')->where(['baby_goods_id' => $goods_id])->select('baby_goods_img')->get()->row_array();
        //查询供应商e_code不存在就添加供应商到e仓否则就修改表
        $e_code = $this->db->from('suppliers')->where(['suppliers_id' => $goods['baby_gooda_supplier']])->select('suppliers_id,suppliers_name,e_code')->get()->row_array();
        if(empty($e_code['e_code'])){
            //code不存在查询E仓信息
            $supplierName = $this->eccang->getSupplierInfo(['condition'=>['supplierName'=>$e_code['suppliers_name']]]);
            //如果supplierName不存在添加
            if(empty($supplierName['data'])){
                $data = ['actionType'=>'ADD','supplier'=>[
                    'supplierCode'=>'G'.$e_code['suppliers_id'],
                    'supplierName'=>$e_code['suppliers_name'],
                    'buyerId'=>$goods['purchase_id'],
                    'level'=>'A',
                    'supplierTeamworkType'=>0,
                    'supplierType'=>1,
                    'pcId'=>3,
                    'accountType'=>2,
                    'payType'=>1,
                    'supplierCarrier'=>1,
                    'shippingMethodIdHead'=>2,
                    'supplierQcException'=>2,
                    'accountProportion'=>100
                ]];
                $re = $this->eccang->syncSupplierInfo($data);
                $e_code['e_code'] = $re['data']['supplierCode'];

            }else{
                $e_code['e_code'] = $supplierName['data'][0]['supplierCode'];
            }
            $this->db->where(['suppliers_name'=>$e_code['suppliers_name']]);
            $this->db->update('suppliers',['e_code'=>$e_code['e_code']]);
        }
        foreach ($goods_sku as $key => $value) {
            if($value['baby_sku_head'] == 01){
                    $option = '不摇头';
                    $options = '209';
                }else{
                    $option = '摇头';
                    $options = '208';
                }
            $size_code = $this->db->from('size')->where(['size_id'=>$value['baby_sku_size']])->select('size_code,e_id')->get()->row_array();
            // $color_zh = $this->db->from('colors')->where(['color_id'=>$value['baby_sku_head']])->select('color_zh,e_id')->get()->row_array();
            $arr['productSku'] = $value['baby_goods_sku'];
            $arr['productTitle'] = $goods['baby_goods_name']. '---' . $option . '/' . $size_code['size_code'];
            $arr['productTitleEn'] = $goods['baby_goods_name'];
            $arr['pdOverseaTypeCn'] = $goods_type['cat_name_zh'];
            $arr['pdOverseaTypeEn'] = $goods_type['cat_name'];
            $arr['productPrice'] = (Float)$goods['baby_goods_price'];
            $arr['personOpraterId'] = $goods['purchase_id'];
            $arr['productColorId'] = $options;
            $arr['productSizeId'] = $size_code['e_id'];
            $arr['spProductAddress'] = $goods['baby_goods_link'];
            $arr['productImgUrlList'] = ['http://sumerbikini.com'.$baby_goods_img['baby_goods_img']];
            $this->eccang->baby_syncEditSaleStatus($arr);
        }
        $this->sys_message->success('成功');
    }

    public function pets_goods_list(){
        $this->load->model('baby_model');
        $this->load->library('pagination');
        $assoc = $this->uri->uri_to_assoc();
        $_param = array(
            'page'           => $assoc['page'] > 0 ? intval($assoc['page']) - 1 : 0,
            'baby_goods_name'     => trim($this->input->get('baby_goods_name')),
            'admin_id'       => intval($this->input->get('admin_id')),
        );

        $config['base_url']         = base_url('Baby/') . 'pets_goods_list/page/';
        $config['total_rows']       = $this->baby_model->baby_goods_num($_param,1);
        $config['num_links']        = 6;
        $config['per_page']         = $this->baby_model->limit_rows;
        $config['use_page_numbers'] = true;
        $this->pagination->initialize($config);

        $baby_goods = $this->baby_model->baby_goods_list($_param,1);
        foreach($baby_goods as $k => $v){
            $shopify = $this->db->from('baby_shopify_goods')->where(['baby_goods_id'=>$v['id']])->get()->result_array();
            $shopify_a = [];
            foreach ($shopify as $key => $value) {
                $siteweb = $this->db->from('siteweb')->where(['web_id'=>$value['web_id']])->select('web_name')->get()->row_array();
                $siteweb['time'] = date('Y-m-d',$value['add_time']);
                $siteweb['web_id'] = $value['web_id'];
                $shopify_a[] = $siteweb;
            }
            $baby_goods[$k]['shopify'] = $shopify_a;
            $suppliers = $this->db->from('suppliers')->where(['suppliers_id'=>$v['baby_gooda_supplier']])->select('suppliers_name')->get()->row_array();
            $baby_goods[$k]['baby_gooda_supplier'] = $suppliers['suppliers_name'];
            $baby_goods_img = $this->baby_model->baby_img($v['id']);
            $baby_goods[$k]['baby_goods_img'] = $baby_goods_img;
            $baby_goods_sku = $this->db->from('baby_goods_sku')->where(['baby_goods_id'=>$v['id'],'baby_is_delete'=>0,'baby_e_update'=>0])->get()->row_array();
            if($baby_goods_sku){
                $baby_goods[$k]['baby_e_update'] = 1;
            }
            $w_query = $this->db->select('on.web_id,on.web_name, sg.add_time')->from('baby_shopify_goods as sg')->join('siteweb as on', 'sg.web_id = on.web_id')->where('sg.baby_goods_id = ' . $v['id'])->get();
            $baby_goods[$k]['wou'] = $w_query->result_array();
        }
        $this->load->model('siteweb_model');
        $siteweb        = $this->siteweb_model->siteweb_option(1);
        $_action_link = array(
            array('href' => base_url('Baby/salegoods'), 'name' => '产品销量'),
            array('href' => base_url('Baby/pets_goods_shelf'), 'name' => '下架产品'),
            array('href' => base_url('Baby/pets_goods'), 'name' => '添加产品'),
        );
        $this->load->model('admin_model');
        $admin_name = $this->admin_model->admin_option(['group' => 10,'is_del'=>0]);
        $_param_option = array(
            'baby_goods_name'  => array(
                'col'         => 'col-lg-3',
                'type'        => 'text',
                'placeholder' => '输入产品名称',
            ),
            'admin_id'    => array(
                'col'         => 'col-lg-2',
                'type'        => 'option',
                'placeholder' => '请选择开发员',
                'option'      => $admin_name,
            ),
        );
        $data = array(
            'siteweb'        => $siteweb,
            'siteweb_json'   => json_encode($siteweb),
            'pages'          => $this->pagination->create_links(),
            'param_option'     => $_param_option,
            'baby_goods' => $baby_goods,
            'action_link'    => $_action_link,
            'param'          => $_param,
        );
        $this->load->view('baby/pets_goods_list', $data);
    }

    public function pets_goods_shelf(){
        $_param_option = array(
            'goods_name' => array(
                'col'         => 'col-lg-3',
                'type'        => 'text',
                'placeholder' => '货号',
            ),
        );
        $_param = array(
            'goods_name'     => trim($this->input->get('goods_name')),
        );
        if($_param['goods_name']){
            $baby_goods = $this->db->from('baby_goods')->where(['baby_goods_sn'=>$_param['goods_name']])->select('baby_goods_sn,baby_add_time,id,baby_goods_link,baby_goods_price')->get()->row_array();
            $baby_goods_img = $this->db->from('baby_goods_img')->where(['baby_goods_id'=>$baby_goods['id']])->select('baby_goods_img')->get()->row_array();
            $baby_goods['baby_goods_img'] = $baby_goods_img['baby_goods_img'];
        }
        if($_POST){
            $this->load->library('shopify_api');
            $goods_id = $this->input->post('goods_id');
            $baby_shopify_goods = $this->db->from('baby_shopify_goods')->where(['baby_goods_id'=>$goods_id])->select('web_id,shopify_id')->get()->result_array();
            foreach ($baby_shopify_goods as $k => $v) {
                $siteweb = $this->db->from('siteweb')->where(['web_id'=>$v['web_id']])->get()->row_array();
                $this->shopify_api->initialize($siteweb);
                $this->shopify_api->goods_delete($v['shopify_id']);
            }
            $this->db->where(['id'=>$goods_id]);
            $this->db->update('baby_goods',['is_on_sale'=>0]);

            $cat_id = $this->db->from('baby_goods')->where(['id'=>$goods_id])->select('cat_id')->get()->row_array();
            $baby_goods_sku = $this->db->from('baby_goods_sku')->where(['baby_goods_id'=>$goods_id,'baby_is_delete'=>0])->select('baby_goods_sku')->get()->result_array();
            $goods_type = $this->db->from('goods_type')->where(['cat_id' => $cat_id['cat_id']])->select('cat_name,cat_name_zh')->get()->row_array();
            foreach($baby_goods_sku as $k => $v){
                $this->load->library('eccang');
                $this->eccang->syncProduct_edit($v['baby_goods_sku'],$goods_type['cat_name_zh'],$goods_type['cat_name'],1);
            }
            
            $this->db->delete('baby_shopify_goods', ['baby_goods_id' => $goods_id]);
            $this->load->library('sys_message');
            $this->sys_message->success('成功!');
        }
        $data = array(
            'goods_list'   => $baby_goods,
            'param_option' => $_param_option,
            'param'        => $_param,
        );
        $this->load->view('baby/pets_goods_shelf', $data);
    }

    public function salegoods(){
       $assoc = $this->uri->uri_to_assoc();
        $_param_option = array(
            'goods_name' => array(
                'col'         => 'col-lg-4',
                'type'        => 'text',
                'placeholder' => '输入产品名称、货号',
            ),
            'order_time' => array(
                'col'         => 'col-lg-4',
                'type'        => 'dpd_time',
                'placeholder' => '销量时间',
            ),
        );
        $siteweb = $this->db->from('siteweb')->where(['type'=>3,'is_delete'=>0])->select('web_id,web_name')->get()->result_array();
        $prepare_state = $this->input->get('prepare_state');
        if(empty($prepare_state)){
            $prepare_state = '';
            foreach ($siteweb as $key => $value) {
                $prepare_state = $prepare_state.','.$value['web_id'];
            }
        }
        
        $this->load->model('baby_model');
        $_param = array(
            'goods_name'       => trim($this->input->get('goods_name')),
            'order_time_start' => trim($this->input->get('order_time_start')),
            'order_time_end'   => trim($this->input->get('order_time_end')),
            'page'             => $assoc['page'] > 0 ? intval($assoc['page']) - 1 : 0,
            'prepare_state'    => $prepare_state,
        );

        $web = $this->db->from('siteweb')->where(['type'=>3,'is_delete'=>0])->where_in('web_id',explode(',',substr($prepare_state,1)))->select('web_id,web_name')->get()->result_array();
        $this->load->library('pagination');
        $config['base_url']         = base_url('Baby/') . 'salegoods/page/';
        $config['total_rows']       = $this->db->from('baby_goods')->where("(baby_goods_sn like '%{$_param['goods_name']}%')")->count_all_results();
        // $config['total_rows']       = $this->baby_model->baby_salegoods_num($_param);
        $config['num_links']        = 6;
        $config['per_page']         = $this->baby_model->limit_rows;
        $config['use_page_numbers'] = true;
        $this->pagination->initialize($config);
        $list = $this->baby_model->baby_salegoods($_param,$prepare_state);

        $data = array(
            'siteweb_is_a' => count($web),
            'goods_list'   => $list,
            'param_option' => $_param_option,
            'siteweb'      => $siteweb,
            'web'      => $web,
            'param'        => $_param,
            'pages'        => $this->pagination->create_links(),
        );

        $this->load->view('baby/salegoods', $data);





        // $this->load->model('goods_model');
        // $this->load->model('goods_sku_model');
        // $this->load->model('order_model');
        // $this->load->model('category_model');
        // $this->load->model('siteweb_model');

        // $this->load->library('pagination');

        // $assoc = $this->uri->uri_to_assoc();

        // $siteweb = $this->siteweb_model->siteweb_option(1);

        // $cate_option = $this->category_model->cate_option();

        // $_param_option = array(
        //     'goods_name' => array(
        //         'col'         => 'col-lg-4',
        //         'type'        => 'text',
        //         'placeholder' => '输入产品名称、货号',
        //     ),
        //     'order_time' => array(
        //         'col'         => 'col-lg-4',
        //         'type'        => 'dpd_time',
        //         'placeholder' => '销量时间',
        //     ),
        // );

        // $_param = array(
        //     'goods_name'       => trim($this->input->get('goods_name')),
        //     'web_order_by'     => intval($_COOKIE['order_sort']),
        //     'cate_name'        => $this->input->get('cate_name'),
        //     'order_time_start' => trim($this->input->get('order_time_start')),
        //     'order_time_end'   => trim($this->input->get('order_time_end')),
        //     'goods_time_start' => trim($this->input->get('goods_time_start')),
        //     'goods_time_end'   => trim($this->input->get('goods_time_end')),
        //     'in_time_start'    => trim($this->input->get('in_time_start')),
        //     'in_time_end'      => trim($this->input->get('in_time_end')),
        //     'page'             => $assoc['page'] > 0 ? intval($assoc['page']) - 1 : 0,
        //     'siteweb'          => intval($this->input->get('siteweb')),
        // );


        // $config['base_url']         = base_url('Product/') . 'salegoods/' . ($_param['order_sorting'] ? "order_sorting/{$_param['order_sorting']}/" : '') . 'page/';
        // $config['total_rows']       = $this->goods_model->goods_num($_param);
        // $config['num_links']        = 6;
        // $config['per_page']         = $this->goods_model->limit_rows;
        // $config['use_page_numbers'] = true;
        // $this->pagination->initialize($config);

        // $_goods_list = $this->goods_sku_model->goods_sku_color_sale($_param);


        // $_siteweb = $this->siteweb_model->siteweb_option(1);

        // $siteweb      = array();
        // $siteweb_is_a = 0;
        // foreach ($_siteweb as $k => $val) {

        //     $siteweb[$k] = array(
        //         'val'    => $val,
        //         'is_acc' => intval($_COOKIE[$val]),
        //     );

        //     $siteweb_is_a += $_COOKIE[$val] == 1 ? 0 : 1;
        // }

        // $data = array(
        //     'goods_list'   => $_goods_list,
        //     'pages'        => $this->pagination->create_links(),
        //     'site_option'  => $this->siteweb_model->siteweb_option(),
        //     'param_option' => $_param_option,
        //     'siteweb'      => $siteweb,
        //     'siteweb_json' => json_encode($_siteweb),
        //     'siteweb_is_a' => $siteweb_is_a,
        //     'param'        => $_param,
        // );

        // $this->load->view('baby/salegoods', $data);
    }
    public function pets_goods(){
            $this->config->load('website_common');
            $this->load->library('session');
            $this->load->model('baby_model');
            $this->load->model('admin_model');
            $this->load->model('suppliers_model');
            // $this->load->model('size_model');
            // $this->load->model('color_model');
            $this->load->library('sys_message');
            $config = $this->config->item('upload');
            $this->load->library('upload', $config);
            $this->load->library('image_lib');
            
            $goods_sn         = $this->baby_model->goods_sn_max($this->session->userdata('admin_id'),1);
            if ($_POST){
                $files_sort = $this->input->post('files_sort');
                !$files_sort && $this->sys_message->wrong('请上传图片!');
                $sku = $this->input->post('sku');
                !array_filter($sku['size']) && $this->sys_message->wrong('请选择尺码!');
                !array_filter($sku['color']) && $this->sys_message->wrong('请选择颜色!');
                $attr      = array(
                    'attr_title' => $this->input->post('attr_title'),
                    'attr_val'   => $this->input->post('attr_val'),
                );
                $data = array(
                    'baby_goods_name'       => trim($this->input->post('cn_name')), //商品名称
                    'baby_channel'          => trim($this->input->post('channel_id')),  //渠道
                    'baby_gooda_supplier'   => trim($this->input->post('suppliers_id')), //供应商ID
                    'baby_admin_id'          => trim($this->input->post('admin_id')), //添加人ID
                    'baby_goods_sn'      => $goods_sn,//货号
                    'baby_goods_link'     => $this->input->post('supp_url'),//商品链接
                    'baby_goods_weight'       => $this->input->post('goods_weight'),//商品重量
                    'baby_goods_enter_price'         => mb_substr($this->input->post('in_price'),1),//进价
                    'baby_goods_price'     => $this->input->post('shop_price'),//售价
                    'baby_goods_describe'         => $this->input->post('summernote'),//描述
                    'baby_add_time'          => time(),//添加时间
                    'baby_type'          => 1,//类型
                    'purchase_id'          => $this->input->post('purchase_id'),//采购员
                    'goods_attr'   => json_encode($attr), //尺码详情
                    'cat_id'       => $this->input->post('cat_id'), //商品分类
                    'ali_goods_name'       => $this->input->post('ali_goods_name'), //1688名称
                );
                $this->db->insert('baby_goods',$data);
                $id = $this->db->insert_id();

                $this->db->from('baby_goods_supp')->where(['supp_url'=>$data['baby_goods_link']])->update('baby_goods_supp',['baby_goods_id'=>$id]);

                // $files_sort = $this->input->post('files_sort');
                // !$files_sort && $this->sys_message->wrong('请上传图片!');
                foreach ($files_sort as $k => $val) {
                    if ($this->upload->do_upload('files' . $k)){
                        $data_images            = $this->upload->data();
                        $img['baby_goods_img'] = $data_images['file_name'];
                        $img['baby_add_time'] = time();
                        $img['baby_goods_id'] = $id;
                        $this->db->insert('baby_goods_img',$img);
                        $baby_sku_img[$k] = $data_images['file_name'];
                    }
                }
                
                
                foreach ($sku['size'] as $key => $val) {
                    if($val){
                        foreach ($sku['color'] as $k => $v){
                            if($v){
                                $sku_img = $baby_sku_img[$sku['color_img'][$k]];
                                $new_size_code  = $this->db->from('size')->where(['size_id'=>$val])->select('new_size_code')->get()->row_array();
                                $new_color_code = $this->db->from('colors')->where(['color_id'=>$v])->select('new_color_code')->get()->row_array();
                                $baby_sku = ['baby_sku_head'=>$v,'baby_sku_size'=>$val,'baby_goods_id'=>$id,'baby_goods_sku'=>$goods_sn.sprintf("%03d", $data['cat_id']).$new_color_code['new_color_code'].$new_size_code['new_size_code'],'baby_sku_img'=>$sku_img];
                                $this->db->insert('baby_goods_sku',$baby_sku);
                            }
                        }
                    }
                }
                $this->sys_message->success('成功!');
                



            }else{
                if(!$this->session->userdata('admin_id')){
                    redirect('Login/');
                }
                $size_option   = $this->db->from('size')->select('size_id,size_code,size_value')->where(['is_delete'=>0])->order_by('size_num asc')->get()->result_array();
                $colors_option = $this->db->from('colors')->select('color_id,color_zh')->where(['is_delete'=>0])->get()->result_array();
                $suppliers_option = $this->suppliers_model->suppliers_option();
                $admin_id         = $this->session->userdata('admin_id');
                
                $admin_name       = $this->admin_model->admin_option();
                $purchase = $this->db->from('purchase')->where(['is_delete' => 0])->get()->result_array();
                $query = $this->db->from('goods_type')->where(['cate_type' => 1,'is_delete'=>1])->get();
                $cate = array();
                foreach ($query->result_array() as $row) {
                    $cate[$row['cat_id']] = $row['cat_name'].'('.$row['cat_name_zh'].')';
                }
                $data = array(
                    'cate_option'      => $cate,
                    'purchase'         => $purchase,
                    'size_option'      => $size_option,
                    'colors_option'    => $colors_option,
                    'suppliers_option' => $suppliers_option,
                    'goods_sn'         => $goods_sn,
                    'admin_name'       => $admin_name,
                    'admin_id'         => $admin_id,
                );

                $this->load->view('baby/pets_goods', $data);
            }
        }

        public function pets_goods_save(){
            if($_POST){
                $this->config->load('website_common');
                $config = $this->config->item('upload');
                $this->load->library('upload', $config);
                $this->load->library('image_lib');
                $this->load->library('sys_message');
                
                $goods_id = $this->input->post('baby_id');
           
                $attr      = array(
                    'attr_title' => $this->input->post('attr_title'),
                    'attr_val'   => $this->input->post('attr_val'),
                );
                $data = array(
                    'baby_goods_name'       => trim($this->input->post('cn_name')), //商品名称
                    'baby_channel'          => trim($this->input->post('channel_id')),  //渠道
                    'baby_gooda_supplier'   => trim($this->input->post('suppliers_id')), //供应商ID
                    'baby_admin_id'          => trim($this->input->post('admin_id')), //添加人ID
                    'baby_goods_sn'      => trim($this->input->post('baby_goods_sn')),//货号
                    'baby_goods_link'     => $this->input->post('supp_url'),//商品链接
                    'baby_goods_weight'       => $this->input->post('goods_weight'),//商品重量
                    'baby_goods_enter_price'         => $this->input->post('baby_goods_enter_price'),//进价
                    'baby_goods_price'     => $this->input->post('baby_goods_price'),//售价
                    'baby_goods_describe'         => $this->input->post('summernote'),//描述
                    'baby_add_time'          => time(),//添加时间
                    'baby_type'          => 1,//添加时间
                    'purchase_id'          => $this->input->post('purchase_id'),//采购员
                    'goods_attr'   => json_encode($attr), //尺码详情
                    'cat_id'       => $this->input->post('cat_id'), //商品分类
                );
                $this->db->where(['id'=>$goods_id]);
                $this->db->update('baby_goods',$data);


                //图片处理
                // $baby_goods_img = $this->db->from('baby_goods_img')->where(['baby_goods_id'=>$goods_id])->select('id,baby_goods_img')->get()->result_array();
                // foreach ($baby_goods_img as $key => $val) {
                //     $img[$val['id']] = $val['baby_goods_img'];
                // }
                $files_sort = $this->input->post('files_sort');
                !$files_sort && $this->sys_message->wrong('请上传图片!');
                $this->db->where(['baby_goods_id'=>$goods_id]);
                $this->db->update('baby_goods_img',['baby_is_delete'=>1]);
                foreach ($files_sort as $k => $val) {

                    if ($val){
                        $this->db->where(['id'=>$val]);
                        $this->db->update('baby_goods_img',['baby_is_delete'=>0]);
                        $baby_goods_img = $this->db->from('baby_goods_img')->where(['id'=>$val])->select('baby_goods_img')->get()->row_array();
                        $files_sort[$k] = $baby_goods_img['baby_goods_img'];
                        // $sku_img_data = array(
                        //     'file_name' => $img[$val],
                        //     'album_id'  => $val,
                        // );
                        // $data['update'][]       = $sku_img_data;
                    } else {
                        if ($this->upload->do_upload('files' . $k)){
                            $data_images            = $this->upload->data();
                            $image['baby_goods_img'] = $data_images['file_name'];
                            $image['baby_add_time'] = time();
                            $image['baby_goods_id'] = $goods_id;
                            $this->db->insert('baby_goods_img',$image);
                            $files_sort[$k] = $data_images['file_name'];
                        }
                    }
                }
                // print_R($files_sort);die;
                //SKU处理
                $sku = $this->input->post('sku');
                $this->db->where(['baby_goods_id'=>$goods_id]);
                $this->db->update('baby_goods_sku',['baby_is_delete'=>1]);
                foreach ($sku['size'] as $k => $v) {
                    $info = $this->db->from('baby_goods_sku')->where(['baby_sku_head'=>$sku['color'][$k],'baby_sku_size'=>$v,'baby_goods_id'=>$goods_id])->get()->row_array();
                    if($info){
                        // echo $sku['img'][$k];die;
                        if($sku['img'][$k] === 'true'){
                            $this->db->where(['id'=>$info['id']]);
                            $this->db->update('baby_goods_sku',['baby_is_delete'=>0]);
                        }else{
                            $this->db->where(['id'=>$info['id']]);
                            $this->db->update('baby_goods_sku',['baby_is_delete'=>0,'baby_sku_img'=>$files_sort[$sku['img'][$k]]]);
                        }
                        
                    }else{
                        $new_size_code  = $this->db->from('size')->where(['size_id'=>$v])->select('new_size_code')->get()->row_array();
                        $new_color_code = $this->db->from('colors')->where(['color_id'=>$sku['color'][$k]])->select('new_color_code')->get()->row_array();
                        $baby_sku = $this->input->post('baby_goods_sn').sprintf("%03d", $data['cat_id']).$new_color_code['new_color_code'].$new_size_code['new_size_code'];
                        $arr['baby_sku_head'] = $sku['color'][$k];
                        $arr['baby_sku_size'] = $v;
                        $arr['baby_add_time'] = time();
                        $arr['baby_goods_sku'] = $baby_sku;
                        $arr['baby_goods_id'] = $goods_id;
                        $arr['baby_sku_img'] = $files_sort[$sku['img'][$k]];
                        $this->db->insert('baby_goods_sku',$arr);
                    }
                }
                $this->sys_message->success('成功!');

            }

            $this->load->model('suppliers_model');
            $this->load->model('admin_model');
            $assoc = $this->uri->uri_to_assoc();
            $this->load->model('baby_model');
            $baby_id = intval($assoc['baby_id']);
            $baby_goods = $this->db->from('baby_goods')->where(['id'=>$baby_id])->get()->row_array();
            $baby_goods_img = $this->db->from('baby_goods_img')->where(['baby_goods_id'=>$baby_goods['id'],'baby_is_delete'=>0])->select('id,baby_goods_img')->get()->result_array();
            foreach ($baby_goods_img as $key => $val) {
                $img[$val['id']] = $val['baby_goods_img'];
            }
            $baby_goods['baby_goods_img'] = $img;
            $baby_goods['baby_goods_sku'] = $this->db->from('baby_goods_sku')->where(['baby_goods_id'=>$baby_goods['id'],'baby_is_delete'=>0])->order_by('sort asc')->select('id,baby_sku_head,baby_sku_size,baby_sku_img,baby_goods_sku')->get()->result_array();
            $size_option   = $this->db->from('size')->select('size_id,size_code,size_value')->where(['is_delete'=>0])->order_by('size_num asc')->get()->result_array();
            $colors_option = $this->db->from('colors')->select('color_id,color_zh')->where(['is_delete'=>0])->get()->result_array();
            $baby_goods['goods_attr'] = json_decode($baby_goods['goods_attr'] , true);
            $purchase = $this->db->from('purchase')->where(['is_delete' => 0])->get()->result_array();
                $query = $this->db->from('goods_type')->where(['cate_type' => 1])->get();
                $cate = array();
                foreach ($query->result_array() as $row) {
                    $cate[$row['cat_id']] = $row['cat_name'].'('.$row['cat_name_zh'].')';
                }
            $data = array(
                'cate_option'      => $cate,
                'purchase'         => $purchase,
                'size_option'      => $size_option,
                'colors_option'    => $colors_option,
                'suppliers_option' => $this->suppliers_model->suppliers_option(),
                'baby_goods' => $baby_goods,
                'admin_name' => $this->admin_model->admin_option(),
            );
            $this->load->view('baby/pets_goods_save', $data);
        }

    public function pets_goods_shopify(){
        $this->load->library('sys_message');
        $this->load->model('siteweb_model');
        $this->load->library('shopify_api');
        $web_id = $this->input->post('web_id');
        $baby_goods_id  = $this->input->post('baby_goods_id');
        $baby_goods = $this->db->from('baby_goods')->where(['id'=>$baby_goods_id])->get()->row_array();
        $goods_sku = $this->db->from('baby_goods_sku')->where(['baby_goods_id' => $baby_goods_id, 'baby_is_delete' => 0])->get()->result_array();
        if(!$goods_sku){
            $this->sys_message->wrong('先上传SKU!');
        }
        if (empty($web_id)){
            $this->sys_message->wrong('请选择站点!');
        }

        foreach ($goods_sku as $key => $value) {
            $size_value = $this->db->from('size')->where(['size_id'=>$value['baby_sku_size']])->select('size_value')->get()->row_array();
            $color_en = $this->db->from('colors')->where(['color_id'=>$value['baby_sku_head']])->select('color_en')->get()->row_array();
            $sku[$key]['option1'] = $size_value['size_value'];
            $sku[$key]['option2'] = $color_en['color_en'];
            $sku[$key]['sku']     = $value['baby_goods_sku'];
            $sku[$key]['price']   = $baby_goods['baby_goods_price'];
        }
        $color['name'] = 'size';
        foreach ($goods_sku as $k => $v) {
            $color_en = $this->db->from('colors')->where(['color_id'=>$v['baby_sku_head']])->select('color_en')->get()->row_array();
            $color['values'][] = $color_en['color_en'];
        }
        $size['name'] = 'color';
        foreach ($goods_sku as $k => $v) {
            $size_value = $this->db->from('size')->where(['size_id'=>$v['baby_sku_size']])->select('size_value')->get()->row_array();
            $size['values'][] = $size_value['size_value'];
        }
        $options[] = $color;
        $options[] = $size;
        $baby_goods_img = $this->db->from('baby_goods_img')->where(['baby_goods_id'=>$baby_goods_id,'baby_is_delete'=>0])->get()->result_array();
        foreach ($baby_goods_img as $key => $value) {
            $img[]['src'] = 'http://sumerbikini.com/'.$value['baby_goods_img'];
        }
        foreach ($web_id as $web_key => $web_val) {
            $baby_shopify_goods = $this->db->from('baby_shopify_goods')->where(['web_id'=>$web_val,'baby_goods_id'=>$baby_goods_id])->get()->row_array();
            if($baby_shopify_goods){
                continue;
            }
            $val['baby_goods_id'] = $baby_goods_id;
            $val['web_id'] = $web_val;
            $val['goods_name'] = $baby_goods['baby_goods_name'];
            $val['goods_sn'] = $baby_goods['baby_goods_sn'];
            $val['goods_price'] = $baby_goods['baby_goods_price'];
            $val['variants'] = json_encode($sku);
            $val['add_time'] = time();
            $this->db->insert('baby_shopify_goods',$val);
            $id = $this->db->insert_id();
            $siteweb = $this->siteweb_model->siteweb_info(array('web_id' => $web_val));
            $this->shopify_api->initialize($siteweb);

            $baby_goods['goods_attr'] = json_decode($baby_goods['goods_attr'], true);
            if ($baby_goods['goods_attr'] && count($baby_goods['goods_attr']['attr_val']) > 1){
                $html = '<table><tr>';
                foreach ($baby_goods['goods_attr']['attr_title'] as $attr_title) {
                    $html .= '<td>' . $attr_title . '</td>';
                }
                $html .= '</tr>';

                $_attr = array();
                foreach ($baby_goods['goods_attr']['attr_val'] as $k => $_row) {
                    foreach ($_row as $_k => $_row_attr) {
                        $_attr[$_k][$k] = $_row_attr;
                    }
                }

                foreach ($_attr as $k_val => $_attr_val) {
                    $html .= '<tr>';
                    foreach ($_attr_val as $k_title => $attr_title) {
                        if ($k_title == 0){
                            $html .= '<td>' . $attr_title . '</td>';
                        } else {
                            $attr_val = explode('-', $attr_title);

                            $a1   = sprintf('%.1f', $attr_val[0] / 2.54);
                            $a2   = $attr_val[1] ? ("-" . sprintf('%.1f', $attr_val[1] / 2.54)) : '';
                            $html .= '<td>' . $a1 . $a2 . '(' . $attr_title . 'cm)' . '</td>';
                        }
                    }
                    $html .= '</tr>';
                }
                $html .= '</table>';
            }
            $baby_goods['baby_goods_describe'] .= $html;


            $baby_shopify_data['baby_goods_name'] = $baby_goods['baby_goods_name'];
            $baby_shopify_data['baby_body_html'] = $baby_goods['baby_goods_describe'];
            $baby_shopify_data['variants'] = $sku;
            $baby_shopify_data['images'] = $img;
            $baby_shopify_data['options'] = $options;
            $info = $this->shopify_api->baby_upload_goods($baby_shopify_data);
            foreach ($info['variants'] as $key => $value) {
                $baby_sku_img = $this->db->from('baby_goods_sku')->where(['baby_goods_sku'=>$value['sku']])->select('baby_sku_img')->get()->row_array();
                foreach ($baby_goods_img as $k => $v) {
                    if($v['baby_goods_img'] == $baby_sku_img['baby_sku_img']){
                        $this->db->where(['baby_goods_sku'=>$value['sku']]);
                        $this->db->update('baby_goods_sku',['baby_shopify_img_id'=>$info['images'][$k]['id'],'baby_shopify_sku_id'=>$value['id']]);
                        continue;
                    }
                }
            }
            //上传SKU图片
            $baby_shopify_img_id = $this->db->from('baby_goods_sku')->where(['baby_goods_id' => $baby_goods_id])->select('baby_shopify_img_id')->group_by('baby_shopify_img_id')->get()->result_array();
            foreach ($baby_shopify_img_id as $key => $value) {
                if (empty($value['baby_shopify_img_id'])){
                    continue;
                }

                $baby_shopify_sku_id = $this->db->from('baby_goods_sku')->where(['baby_shopify_img_id' => $value['baby_shopify_img_id']])->select('baby_shopify_sku_id')->get()->result_array();
                $variant_ids    = [];

                foreach ($baby_shopify_sku_id as $k => $v) {
                    $variant_ids[] = $v['baby_shopify_sku_id'];
                }
                $res = array('image' => array('id' => $value['baby_shopify_img_id'], 'variant_ids' => $variant_ids));
                $this->shopify_api->shopify_sku_img($info['id'], $value['baby_shopify_img_id'], $res);
            }
            $this->db->where(['id'=>$id]);
            $this->db->update('baby_shopify_goods',['shopify_id'=>$info['id']]);
        }
        $this->sys_message->success('成功!');
    }

    public function pets_goods_sku_examine(){

        $this->load->library('sys_message');
        if ( !$_SESSION['admin_id']){
            $this->sys_message->wrong('刷新页面');
        }
        // if ($_SESSION['admin_id'] != 2 && $_SESSION['admin_id'] != 13 && $_SESSION['admin_id'] != 33){
        //     $this->sys_message->wrong('负责人才能审核');
        // }

        $goods_id = $this->input->post('goods_id');
        if (empty($goods_id)){
            $this->sys_message->wrong('刷新页面');
        }
        $array = $this->db->from('baby_goods_sku')->where(['baby_goods_id' => $goods_id, 'baby_is_delete' => 0,'baby_e_update'=> 0])->get()->result_array();
        foreach ($array as $k => $v) {
            $baby_goods = $this->db->from('baby_goods')
                           ->where(['id' => $v['baby_goods_id']])
                           ->get()->row_array();
            $type       = $this->db->from('goods_type')
                                       ->where(['cat_id' => $baby_goods['cat_id']])
                                       ->select('cat_name_zh,cat_name,cate_code')
                                       ->get()->row_array();
            $suppliers_name = $this->db->from('suppliers')
                           ->where(['suppliers_id' => $baby_goods['baby_gooda_supplier']])
                           ->select('suppliers_name,suppliers_id')
                           ->get()->row_array();
            $goods_album = $this->db->from('baby_goods_img')
                    ->where(['baby_goods_id'=>$v['baby_goods_id'],'baby_is_delete'=>0])
                    ->select('baby_goods_img')
                    ->get()->row_array();
            $this->load->library('eccang');
            $info = $this->eccang->getSupplierInfo(['condition' => ['supplierName' => trim($suppliers_name['suppliers_name'])]]);
            if ( !empty($info['data'])){
                $defaultSupplierCode = $info['data'][0]['supplierCode'];
            } else {
                $defaultSupplierCode = 'G' . $suppliers_name['suppliers_id'];
                $data                = [
                    'actionType' => 'ADD',
                    'supplier'   => [
                        'supplierCode'         => $defaultSupplierCode,
                        'supplierName'         => $suppliers_name['suppliers_name'],
                        'buyerId'              => $suppliers_name['suppliers_id'] % 2 == 1 ? 583 : 609,
                        'level'                => 'A',
                        'supplierTeamworkType' => 0,
                        'supplierType'         => 1,
                        'pcId'                 => 3,
                        'accountType'          => 2,
                        'payType'              => 1,
                        'supplierCarrier'      => 1,
                        'shippingMethodIdHead' => 2,
                        'supplierQcException'  => 2,
                        'accountProportion'    => 100,
                    ],
                ];
                $suppliers_e         = $this->eccang->syncSupplierInfo($data);
                $this->db->where(['suppliers_id' => $suppliers_name['suppliers_id']]);
                $this->db->update('suppliers', ['e_code' => $suppliers_e['data']['supplierCode']]);
            }

            $colors    = $this->db->from('colors')->where(['color_id' => $v['baby_sku_head']])->select('color_zh,e_id')->get()->row_array();
            $size      = $this->db->from('size')->where(['size_id' => $v['baby_sku_size']])->select('size_code,e_id')->get()->row_array();
            $res       = [];
            $res['actionType']                 = 'ADD';
            $res['productSku']                 = $v['baby_goods_sku'];
            $res['productTitle']               = $baby_goods['baby_goods_name'] . '---' . $colors['color_zh'] . '/' . $size['size_code'];
            $res['productTitleEn']             = $baby_goods['baby_goods_name'];
            $res['pdOverseaTypeCn']            = $type['cat_name_zh'];
            $res['pdOverseaTypeEn']            = $type['cat_name'];
            $res['productDeclaredValue']       = 1.5;
            $res['pdDeclareCurrencyCode']      = 'USD';
            $res['productPurchaseValue']       = (Float)$baby_goods['baby_goods_enter_price'];
            $res['productPurchaseValueNotTax'] = (Float)$baby_goods['baby_goods_enter_price'];
            $res['currencyCode']               = 'RMB';
            $res['productWeight']              = $baby_goods['baby_goods_weight'] < 50 ? 0.05 : $baby_goods['baby_goods_weight'] / 1000;
            $res['defaultSupplierCode']        = $defaultSupplierCode;
            $res['productPrice']               = (Float)$baby_goods['baby_goods_price'];
            $res['oprationType']               = 2;
            $res['productStatus']              = 1;
            $res['saleStatus']                 = 2;
            $res['hsCode']                     = $type['cate_code'];
            $res['productLength']              = 1;
            $res['productWidth']               = 1;
            $res['productHeight']              = 1;
            $res['personOpraterId']            = $baby_goods['purchase_id'];
            $res['productColorId']             = $colors['e_id'];
            $res['productSizeId']              = $size['e_id'];
            $res['spMinQty']                   = 1;
            $res['spProductAddress']           = $baby_goods['baby_goods_link'];
            $res['productImgUrlList']          = ['http://sumerbikini.com'.$goods_album['baby_goods_img']];
            //print_R($res);die;
            $info                              = $this->eccang->add_syncProduct($res);
            if ($info['code'] == 200 || ($info['code'] == 500 && $info['error'][0]['errorMsg'] == '产品' . $v['baby_goods_sku'] . '已经存在')){//处理e仓接口易仓导致商品已经创建成功
                $this->db->where(['id' => $v['id']]);
                $this->db->update('baby_goods_sku', ['baby_e_update' => 1]);
            }
        }
        $this->sys_message->success('成功');
    }

    public function shopify_goods_delete(){
        $this->load->library('sys_message');
        $web_id   = $this->input->post('web_id');
        $goods_id = $this->input->post('goods_id');
        if ( !$web_id || !$goods_id){
            $this->sys_message->wrong('删除失败!');
        }
        $res = $this->db->delete('baby_shopify_goods', ['web_id' => $web_id, 'baby_goods_id' => $goods_id]);
        if ($res){
            $this->sys_message->success('成功！');
        } else {
            $this->sys_message->wrong('删除失败!');
        }

    }

    /**
     * e仓规格图片更新
     */
    public function syncProduct_attr_img()
    {
        $this->load->library('sys_message');
        $this->load->library('eccang');
        $goods_id = $this->input->post('goods_id');
        $goods_sku = $this->db->from('baby_goods_sku')->where(['baby_goods_id' => $goods_id])->get()->result_array();
        $goods = $this->db->from('baby_goods')->where(['id' => $goods_id])->select('baby_goods_price,cat_id,baby_goods_name,baby_gooda_supplier,purchase_id,baby_goods_link')->get()->row_array();
        $goods_type = $this->db->from('goods_type')->where(['cat_id' => $goods['cat_id']])->select('cat_name,cat_name_zh')->get()->row_array();
        $baby_goods_img = $this->db->from('baby_goods_img')->where(['baby_goods_id' => $goods_id])->select('baby_goods_img')->get()->row_array();
        //查询供应商e_code不存在就添加供应商到e仓否则就修改表
        $e_code = $this->db->from('suppliers')->where(['suppliers_id' => $goods['baby_gooda_supplier']])->select('suppliers_id,suppliers_name,e_code')->get()->row_array();
        if(empty($e_code['e_code'])){
            //code不存在查询E仓信息
            $supplierName = $this->eccang->getSupplierInfo(['condition'=>['supplierName'=>$e_code['suppliers_name']]]);
            //如果supplierName不存在添加
            if(empty($supplierName['data'])){
                $data = ['actionType'=>'ADD','supplier'=>[
                    'supplierCode'=>'G'.$e_code['suppliers_id'],
                    'supplierName'=>$e_code['suppliers_name'],
                    'buyerId'=>$goods['purchase_id'],
                    'level'=>'A',
                    'supplierTeamworkType'=>0,
                    'supplierType'=>1,
                    'pcId'=>3,
                    'accountType'=>2,
                    'payType'=>1,
                    'supplierCarrier'=>1,
                    'shippingMethodIdHead'=>2,
                    'supplierQcException'=>2,
                    'accountProportion'=>100
                ]];
                $re = $this->eccang->syncSupplierInfo($data);
                $e_code['e_code'] = $re['data']['supplierCode'];

            }else{
                $e_code['e_code'] = $supplierName['data'][0]['supplierCode'];
            }
            $this->db->where(['suppliers_name'=>$e_code['suppliers_name']]);
            $this->db->update('suppliers',['e_code'=>$e_code['e_code']]);
        }
        foreach ($goods_sku as $key => $value) {
            $size_code = $this->db->from('size')->where(['size_id'=>$value['baby_sku_size']])->select('size_code,e_id')->get()->row_array();
            $color_zh = $this->db->from('colors')->where(['color_id'=>$value['baby_sku_head']])->select('color_zh,e_id')->get()->row_array();
            $arr['productSku'] = $value['baby_goods_sku'];
            $arr['productTitle'] = $goods['baby_goods_name']. '---' . $color_zh['color_zh'] . '/' . $size_code['size_code'];
            $arr['productTitleEn'] = $goods['baby_goods_name'];
            $arr['pdOverseaTypeCn'] = $goods_type['cat_name_zh'];
            $arr['pdOverseaTypeEn'] = $goods_type['cat_name'];
            $arr['productPrice'] = (Float)$goods['baby_goods_price'];
            $arr['personOpraterId'] = $goods['purchase_id'];
            $arr['productColorId'] = $color_zh['e_id'];
            $arr['productSizeId'] = $size_code['e_id'];
            $arr['spProductAddress'] = $goods['baby_goods_link'];
            $arr['productImgUrlList'] = ['http://sumerbikini.com'.$baby_goods_img['baby_goods_img']];
            $this->eccang->baby_syncEditSaleStatus($arr);
        }
        $this->sys_message->success('成功');
    }



    public function goods_web_up(){
            ignore_user_abort();
            set_time_limit(0);

            $this->load->library('sys_message');
            $this->load->model('shopify_goods_model');
            $this->load->model('siteweb_model');
            $this->load->model('goods_model');
            $this->load->library('shopify_api');
            $this->load->model('tagslist_model');

            $goods_id = $this->input->post('goods_id');

            $update_web_id = $this->input->post('web_id');
            if (empty($update_web_id)){
                $this->sys_message->wrong('先选择站点!');
            }

            $goods_sku = $this->db->from('baby_goods_sku')->where(['baby_goods_id' => $goods_id, 'baby_e_update' => 1, 'baby_is_delete' => 0])->get()->result_array();
            if (!$goods_sku){
                $this->sys_message->wrong('先上传SKU!');
            }
            $goods = $this->db->from('baby_goods')->where(['id' => $goods_id])->get()->row_array();
            !$goods && $this->sys_message->wrong('产品不存在!');

            $siteweb = $this->db->from('siteweb as s')->select('s.*,sg.id,sg.shopify_id')->join('baby_shopify_goods as sg', 's.web_id = sg.web_id', 'left')->where("sg.baby_goods_id = '{$goods_id}'")->get()->result_array();


            foreach ($siteweb as $key => $value) {
                if ( !in_array($value['web_id'], $update_web_id)){
                    unset($siteweb[$key]);
                }
            }

            if (empty($siteweb)){
                $this->sys_message->wrong('商品没有上传过站点');
            }

            //查询商品图片
            $baby_goods_img = $this->db->select('baby_goods_img')->from('baby_goods_img')->where(['baby_goods_id' => $goods_id])->get()->result_array();
            
            foreach ($baby_goods_img AS $row) {
                $images[] = array(
                    'src' => 'http://sumerbikini.com'.$row['baby_goods_img'],
                );
            }

            $goods['goods_attr'] = json_decode($goods['goods_attr'], true);

            if ($goods['goods_attr'] && count($goods['goods_attr']['attr_val']) > 1){
                $_attr = array();
                $html  = '<table><tr>';
                foreach ($goods['goods_attr']['attr_title'] as $attr_title) {
                    $html .= '<td>' . $attr_title . '</td>';
                }
                $html .= '</tr>';

                foreach ($goods['goods_attr']['attr_val'] as $k => $_row) {
                    foreach ($_row as $_k => $_row_attr) {
                        $_attr[$_k][$k] = $_row_attr;
                    }
                }

                foreach ($_attr as $k_val => $_attr_val) {
                    $html .= '<tr>';
                    foreach ($_attr_val as $k_title => $attr_title) {
                        if ($k_title == 0){
                            $html .= '<td>' . $attr_title . '</td>';
                        } else {
                            $attr_val = explode('-', $attr_title);

                            $a1   = sprintf('%.1f', $attr_val[0] / 2.54);
                            $a2   = $attr_val[1] ? ("-" . sprintf('%.1f', $attr_val[1] / 2.54)) : '';
                            $html .= '<td>' . $a1 . $a2 . '(' . $attr_title . 'cm)' . '</td>';
                        }
                    }
                    $html .= '</tr>';
                }
                $html .= '</table>';
            }

            $data = array(
                'shop_price'       => $goods['baby_goods_price'],
                'body_html'        => $goods['body_html'] . $html,
                'is_sales'         => 1,
                'variants'         => array(),
                'images'           => $images,
            );

            foreach ($siteweb as $key => $_web) {

                $this->shopify_api->initialize($_web);

                $data['shopify_id'] = $_web['shopify_id'];
                $data['web_id']     = $_web['web_id'];
                $data['goods_name'] = $goods['baby_goods_name'];
                $variants = array();
                foreach ($goods_sku as $row) {
                    $size_value = $this->db->from('size')->where(['size_id'=>$row['baby_sku_size']])->select('size_value')->get()->row_array();
                    $color_en = $this->db->from('colors')->where(['color_id'=>$row['baby_sku_head']])->select('color_en')->get()->row_array();
                    $variants[] = array(
                        'option1' => $size_value['size_value'],
                        'option2' => $color_en['color_en'],
                        'sku'     => $row['baby_goods_sku'],
                        'price'   => $goods['baby_goods_price'],
                    );
                }
                $data['variants'] = $variants;

                $response = $this->shopify_api->update_up_goods($data);
                // //处理shopify图片ID
                // foreach ($data['images'] as $key => $value) {
                //     //shopify 图片
                //     $this->db->where(['album_url' => substr($value['src'], 22)]);
                //     $this->db->update('goods_sku', ['img_shopify_id' => $response['images'][$key]['id']]);
                // }
                // //处理shopify sku ID
                // foreach ($data['variants'] as $key => $value) {
                //     foreach ($response['variants'] as $k => $v) {
                //         if ($v['sku'] == $value['sku']){
                //             $this->db->where(['sku_num_sn' => substr($value['sku'], 2)]);
                //             $this->db->update('goods_sku', ['sku_shopify_id' => $v['id']]);
                //         }
                //     }
                // }
                // //上传SKU图片
                // $img_shopify_id = $this->db->from('goods_sku')->where(['goods_id' => $goods_id])->select('img_shopify_id')->group_by('img_shopify_id')->get()->result_array();
                // foreach ($img_shopify_id as $key => $value) {
                //     if (empty($value['img_shopify_id'])){
                //         continue;
                //     }

                //     $sku_shopify_id = $this->db->from('goods_sku')->where(['img_shopify_id' => $value['img_shopify_id']])->select('sku_shopify_id')->get()->result_array();
                //     $variant_ids    = [];

                //     foreach ($sku_shopify_id as $k => $v) {
                //         $variant_ids[] = $v['sku_shopify_id'];
                //     }
                //     $res = array('image' => array('id' => $value['img_shopify_id'], 'variant_ids' => $variant_ids));
                //     $this->shopify_api->shopify_sku_img($response['id'], $value['img_shopify_id'], $res);
                // }
            }
            $this->sys_message->success('成功!');
        }
    
        public function ajax_ali_goods(){
            $this->load->library('sys_message');
            $this->load->library('ali');
            $url = trim($this->input->post('ali_url'));
            $supp_goods = $this->db->from('baby_goods_supp')->where(['supp_url'=>$url])->get()->row_array();
            if($supp_goods){
                $_ali_goods = json_decode($supp_goods['goods_json'], true);
                $skuProps = $_ali_goods['skuProps'];
            }else{
                $info       = $this->ali->pu_goods($url);
                $_ali_goods = $info['data'];
                if(is_array($_ali_goods)){
                    $ali_price = explode('-', $_ali_goods['displayPrice']);
                    $skuProps  = $_ali_goods['skuProps'];

                    // $attributes = array();
                    // foreach ($_ali_goods['attributes'] as $row) {
                    //     $row                 = explode(':', $row);
                    //     $attributes[$row[0]] = $row[1];
                    // }

                    $supp_goods = array(
                        'in_price'   => end($ali_price),
                        'cn_name'    => $_ali_goods['title'],
                        'supp_url'   => $url,
                        'goods_json' => json_encode($_ali_goods),
                        'add_time'    => time(),
                    );
                    $this->db->insert('baby_goods_supp',$supp_goods);
                    // $supp_goods['g_supp_id'] = $this->suppliers_model->insert_goods_supp($supp_goods);
                }else{
                    $this->sys_message->wrong('获取失败!');
                }
            }

            $re_ali_goods = array(
                'ali_price' => $supp_goods['in_price'],
                //'ali_sn'    => $supp_goods['supp_sn'],
                'skuProps'  => $skuProps,
                'title'     => $supp_goods['cn_name'],
                // 'g_supp_id' => $supp_goods['g_supp_id'],
            );
            $this->sys_message->success('成功!', $re_ali_goods);





            // $this->load->model('suppliers_model');

            // $this->load->library('ali');
            // $this->load->library('sys_message');
            // $url = trim($this->input->post('ali_url'));

            // !$url && $this->sys_message->wrong('请填写URL!');

            // $supp_goods = $this->suppliers_model->goods_supp_info(array('supp_url' => $url, 'prepare_id' => ''));

            // if ($supp_goods){
            //     // if($supp_goods['goods_id'] || $supp_goods['prepare_id']){
            //     //     $this->sys_message->wrong('1688链接已经填写过啦!');
            //     // }
            //     $_ali_goods = json_decode($supp_goods['goods_json'], true);

            //     $skuProps = $_ali_goods['skuProps'];
            // } else {
            //     $info       = $this->ali->pu_goods($url);
            //     $_ali_goods = $info['data'];

            //     if (is_array($_ali_goods)){
            //         $ali_price = explode('-', $_ali_goods['displayPrice']);
            //         $skuProps  = $_ali_goods['skuProps'];

            //         $attributes = array();
            //         foreach ($_ali_goods['attributes'] as $row) {
            //             $row                 = explode(':', $row);
            //             $attributes[$row[0]] = $row[1];
            //         }

            //         $supp_goods = array(
            //             'in_price'   => end($ali_price),
            //             'cn_name'    => $_ali_goods['title'],
            //             'supp_url'   => $url,
            //             'goods_json' => json_encode($_ali_goods),
            //             'supp_sn'    => $attributes['货号'],
            //         );

            //         $supp_goods['g_supp_id'] = $this->suppliers_model->insert_goods_supp($supp_goods);
            //     } else {
            //         $this->sys_message->wrong('获取失败!');
            //     }

            // }

            // $re_ali_goods = array(
            //     'ali_price' => $supp_goods['in_price'],
            //     'ali_sn'    => $supp_goods['supp_sn'],
            //     'skuProps'  => $skuProps,
            //     'title'     => $supp_goods['cn_name'],
            //     'g_supp_id' => $supp_goods['g_supp_id'],
            // );

            // $this->sys_message->success('成功!', $re_ali_goods);
        }

    public function goods_color()
    {
        $goods_id = $this->input->post('goods_id');
        $baby_goods_sku = $this->db->from('baby_goods_sku')->where(['baby_goods_id'=>$goods_id])->order_by('sort asc')->group_by('baby_sku_head')->get()->result_array();
        foreach ($baby_goods_sku as $k => $v) {
            $color_en = $this->db->from('colors')->where(['color_id'=>$v['baby_sku_head']])->select('color_en')->get()->row_array();
            $baby_goods_sku[$k]['color'] = $color_en['color_en'];
        }
        echo json_encode($baby_goods_sku);
    }

    public function update_color()
    {
        $this->load->library('sys_message');
        $val = $this->input->post('arr');
        foreach ($val as $k => $v) {
            $this->db->where(['baby_goods_id' => $v['goods_id'], 'baby_sku_head' => $v['color']])->update('baby_goods_sku', ['sort' => $v['sort']]);
        }
        $this->sys_message->success('成功！', $data);
    }

    public function img_list(){
        $goods_id = $this->input->post('goods_id');
        $baby_goods_img = $this->db->from('baby_goods_img')->where(['baby_goods_id'=>$goods_id,'baby_is_delete'=>0])->get()->result_array();
        echo json_encode($baby_goods_img);
    }

    public function zhutu_img(){
        $goods_id = $this->input->post('goods_id');
        $img_id = $this->input->post('img_id');
        $this->db->where(['baby_goods_id' => $goods_id])->update('baby_goods_img', ['sort' => 0]);
        $this->db->where(['id' => $img_id])->update('baby_goods_img', ['sort' => 1]);
    }

    public function baby_statistics(){
        // $this->load->model('goods_model');
        // $this->load->model('goods_sku_model');
        // $this->load->model('order_model');
        // $this->load->model('category_model');
        $this->load->model('siteweb_model');

        $this->load->library('pagination');
        $this->load->model('baby_model');
        $assoc = $this->uri->uri_to_assoc();

        $siteweb = $this->siteweb_model->siteweb_option(1,['type'=>3]);
        // $cate_option = $this->category_model->cate_option();
        $this->load->model('admin_model');
        $admin_name = $this->admin_model->admin_option(['group' => 10,'is_del'=>0]);
        $_param_option = array(
            'goods_name' => array(
                'col'         => 'col-lg-4',
                'type'        => 'text',
                'placeholder' => '输入产品名称、货号',
            ),
            // 'cate_name'  => array(
            //     'col'         => 'col-lg-2',
            //     'type'        => 'option',
            //     'placeholder' => '选择分类',
            //     'option'      => $cate_option,
            // ),
            'order_time' => array(
                'col'         => 'col-lg-4',
                'type'        => 'dpd_time',
                'placeholder' => '销量时间',
            ),
            // 'goods_time' => array(
            //     'col'         => 'col-lg-4',
            //     'type'        => 'dpd_time',
            //     'placeholder' => '上传时间',
            // ),
            // 'in_time'    => array(
            //     'col'         => 'col-lg-4',
            //     'type'        => 'dpd_time',
            //     'placeholder' => '上架时间',
            // ),
            // 'siteweb'    => array(
            //     'col'         => 'col-lg-2',
            //     'type'        => 'option',
            //     'placeholder' => '选择网站',
            //     'option'      => $siteweb,
            // ),
            'admin_id'    => array(
                'col'         => 'col-lg-2',
                'type'        => 'option',
                'placeholder' => '请选择开发员',
                'option'      => $admin_name,
            ),
        );


        $_param = array(
            'goods_name'       => trim($this->input->get('goods_name')),
            // 'web_order_by'     => intval($_COOKIE['order_sort']),
            // 'cate_name'        => $this->input->get('cate_name'),
            'order_time_start' => trim($this->input->get('order_time_start')),
            'order_time_end'   => trim($this->input->get('order_time_end')),
            // 'goods_time_start' => trim($this->input->get('goods_time_start')),
            // 'goods_time_end'   => trim($this->input->get('goods_time_end')),
            // 'in_time_start'    => trim($this->input->get('in_time_start')),
            // 'in_time_end'      => trim($this->input->get('in_time_end')),
            'page'             => $assoc['page'] > 0 ? intval($assoc['page']) - 1 : 0,
            // 'siteweb'          => intval($this->input->get('siteweb')),
            'admin_id'       => intval($this->input->get('admin_id')),
        );
        if($_param['goods_name']){
            $this->db->where(['baby_goods_sn'=>$_param['goods_name']]);
        }
        if($_param['admin_id'] > 0){
            $this->db->where(['baby_admin_id'=>$_param['admin_id']]);
        }
        $baby_goods = $this->db->from('baby_goods')->order_by('id desc')->where(['baby_type'=>1])->limit(15,15 * $_param['page'])->select('id,baby_goods_sn')->get()->result_array();
        foreach ($baby_goods as $k => $v) {
            $baby_goods_img = $this->db->from('baby_goods_img')->where(['baby_goods_id'=>$v['id'],'sort'=>1])->select('baby_goods_img')->get()->row_array();
            if(empty($baby_goods_img)){
                $baby_goods_img = $this->db->from('baby_goods_img')->where(['baby_goods_id'=>$v['id']])->select('baby_goods_img')->get()->row_array();
            }
            $baby_goods[$k]['baby_goods_img'] = $baby_goods_img['baby_goods_img'];
        }
        foreach ($baby_goods as $key => $val) {
            $count = 0;
            foreach ($siteweb as $web_k => $web_v) {
                if ($_param['order_time_start'] && $_param['order_time_end']) {
                        $this->db->where("oi.add_time >= '{$_param['order_time_start']}' and oi.add_time <= '{$_param['order_time_end']}'");
                    }
                $order_count = $this->db->from('order_goods as og')->join('order_info as oi', 'oi.order_id = og.order_id')->where('og.goods_id = ' . $val['id'])->where('oi.web_id = ' . $web_k)->get()->result_array();
                $count = count($order_count) + $count;
                $baby_goods[$key][$web_v] = count($order_count);
            }
            $baby_goods[$key]['count'] = $count;
        }


// print_R($baby_goods);die;
        // $config['base_url']         = base_url('baby/') . 'baby_statistics/' . ($_param['order_sorting'] ? "order_sorting/{$_param['order_sorting']}/" : '') . 'page/';
        // $config['total_rows']       = $this->goods_model->goods_num($_param);
        // $config['num_links']        = 6;
        // $config['per_page']         = $this->goods_model->limit_rows;
        // $config['use_page_numbers'] = true;
        // $this->pagination->initialize($config);

        $config['base_url']         = base_url('Baby/') . 'baby_statistics/page/';
        $config['total_rows']       = $this->baby_model->statistics_num($_param);
        $config['num_links']        = 6;
        $config['per_page']         = $this->baby_model->limit_rows;
        $config['use_page_numbers'] = true;
        $this->pagination->initialize($config);



        //$_goods_list = $this->goods_sku_model->goods_sku_color_sale($_param);

        // $_action_link = array(
        //     array('href' => base_url('Order/order_five_sum'), 'name' => '新销售数据'),
        //     array('href' => base_url('SalesCount/total_sales'), 'name' => '总销量'),
        //     array('href' => base_url('SalesCount/index'), 'name' => '新版销量图'),
        // );
        // $_siteweb = $this->siteweb_model->siteweb_option(1,['type'=>3]);
// print_R($siteweb);die;
//         $siteweb      = array();
//         //$siteweb_is_a = 0;
//         foreach ($_siteweb as $k => $val) {

//             $siteweb[$k] = array(
//                 'val'    => $val,
//                 'is_acc' => intval($_COOKIE[$val]),
//             );

//             //$siteweb_is_a += $_COOKIE[$val] == 1 ? 0 : 1;
//         }

        $data = array(
            'baby_goods_list' => $baby_goods,
            //'goods_list'   => $_goods_list,
            // 'action_link'  => $_action_link,
            'pages'        => $this->pagination->create_links(),
            'site_option'  => $this->siteweb_model->siteweb_option(),
            'param_option' => $_param_option,
            'siteweb'      => $siteweb,
            // 'siteweb_json' => json_encode($_siteweb),
            'siteweb_is_a' => count($siteweb),
            'param'        => $_param,
        );

        $this->load->view('baby/baby_statistics', $data);
    }

    public function goods_up_all(){
        $goods_id = $this->input->post('str');
        $web_id = $this->input->post('web_id');
        foreach ($goods_id as $key => $value) {
            if(!empty($value)){
                $info = $this->db->from('baby_update')->where(['baby_goods_id'=>$value,'web_id'=>$web_id[0],'is_up'=>0])->get()->row_array();
                if(empty($info)){
                    $data = ['baby_goods_id'=>$value,'web_id'=>$web_id[0]];
                    $this->db->insert('baby_update',$data);
                    
                }
            }
        }
        $this->load->library('sys_message');
        $this->sys_message->success('成功!');
    }
    }
